package com.tedu.ivos.dict.service.impl;

import com.tedu.ivos.dict.mapper.DictMapper;
import com.tedu.ivos.dict.pojo.dto.DictQuery;
import com.tedu.ivos.dict.pojo.dto.DictSaveParam;
import com.tedu.ivos.dict.pojo.entity.Dict;
import com.tedu.ivos.dict.pojo.vo.DictVO;
import com.tedu.ivos.dict.service.DictService;
import com.tedu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> selectDict(DictQuery dictQuery) {
        log.debug("查询字典信息业务，参数：{}", dictQuery);
        List<DictVO> list = dictMapper.select(dictQuery);

        return list;
    }

    @Override
    public void saveDict(DictSaveParam dictSaveParam) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam, dict);
        if (dict.getId() == null){
            log.debug("新增字典业务，参数：{}", dictSaveParam);
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        } else {
            log.debug("编辑字典业务，参数：{}", dictSaveParam);
            dict.setUpdateTime(new Date());
            dictMapper.updateDict(dict);
        }

    }

    @Override
    public void deleteDict(Long id) {
        log.debug("删除字典业务，参数：{}", id);
        dictMapper.deleteDict(id);
        dictOptionMapper.deleteByDict(id);
    }

    @Override
    public DictVO selectById(Long id) {
        log.debug("编辑字典业务，参数：{}", id);
        DictVO dictVO = dictMapper.selectById(id);

        return dictVO;
    }
}
